%%%-----------------------------------
%%% @Module  : pt_10
%%% @Author  : kxp
%%% @Created : 2016.07.27
%%% @Description: 10帐户信息
%%%-----------------------------------
-module(pt_10).
-export([read/2, write/2,time_to_string/1]).
-include("common.hrl").
-include("proto_all.hrl").

-define(LOGIN_AGENT, [0, 1, 301, 302, 303, 501, 502, 601, 602, 701, 702, 801, 802]).
%%
%%客户端 -> 服务端 ----------------------------
%%

%%登陆
read(10000, Bin) ->
	{ClientData, _} = proto_read:unpack(c2s_account_connect, Bin),	
	{c2s_account_connect ,Fieldtick, Fieldsign, Fieldagent, Fieldaccount, Fieldaccname, Fieldpassword, Fielddid, Fielddata} = ClientData,
    {ok, [Fieldtick, Fieldsign, Fieldagent, Fieldaccount, Fieldaccname, Fieldpassword, Fielddid, Fielddata]};

%%登录协议
read(10001, Bin) ->
	NewBin = lib_crypto:decrypt(Bin),
    {ClientData, _} = proto_read:unpack(c2s_account_login, NewBin),
	{c2s_account_login ,Fieldtick, Fieldsign, Fieldversion, Fieldagent, Fieldaccount_id,
     Fieldaccname, Fieldpassword, Fieldnick,Fieldchannel, Fielddevice, Fielddid,
     AgentCode,WxOpenId,WxUnionId,SdkUid} = ClientData,
	
	case lists:member(Fieldagent, ?LOGIN_AGENT) of
		true ->			
    		{ok, [Fieldtick, Fieldsign, Fieldversion, Fieldagent, Fieldaccount_id, Fieldaccname, Fieldpassword, Fieldnick, 
                  Fieldchannel, Fielddevice, Fielddid,AgentCode,WxOpenId,WxUnionId,SdkUid]};
		_ ->
			lib_warn:warn(?MODULE, 10001, <<"10001协议解密失败"/utf8>>, [{player_agent, Fieldagent}, {default, ?LOGIN_AGENT}], [Bin]),
			?ERROR_MSG("~s pt_10 10001 decrypt error, DecClientData:~p~n",[misc:time_format(game_timer:now()), ClientData]),
			{error, decrypt_error}
	end;

%% 10008心跳包
read(10008, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_heart_beat, Bin),
	{c2s_heart_beat ,Syc} = ClientData,
    {ok, [Syc]};

%% 10012绑定关系
read(10012, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_agentcode_req, Bin),
    {c2s_agentcode_req, AgentCode,AgentPhone} = ClientData,
    {ok, [AgentCode,AgentPhone]};

read(10020, Bin) ->
    {ClientData, _} = proto_read:unpack(c2s_client_version_req, Bin),
    {c2s_client_version_req , Version, Channel} = ClientData, 
    {ok, [Version, Channel]};

read(_Cmd, _R) ->
    ?ERROR_MSG("~s pt_10 reading cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), _Cmd, _R]),
    {error, no_match}.

%%
%%服务端 -> 客户端 ------------------------------------
%%

%%登陆返回
write(10000, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_account_connect, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10000, ServerData)};

%% 10001登录返回
write(10001, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_account_login, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10001, ServerData)};

%%心跳包
write(10008, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_heart_beat, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10008, ServerData)};

%%被下线通知
write(10007, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_account_logout, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10007, ServerData)};

%% 代理
write(10011, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_agent_info, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10011, ServerData)};


%% 代理邀请码
write(10012, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_agentcode_req, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10012, ServerData)};

write(10020, [Seq, RecordData]) ->
    MsgPack = proto_write:pack(s2c_client_version_req, RecordData),
    ServerData = <<Seq:32, MsgPack/binary>>,
    {ok, pt:pack(10020, ServerData)};

write(Cmd, _R) ->
    ?ERROR_MSG("~s pt_10 writting cmd_[~p], Data:~p~n",[misc:time_format(game_timer:now()), Cmd, _R]),
    {ok, pt:pack(0, <<>>)}.

time_to_string(Time) ->
	{{_Year, Month, Day},{Hour, M, _S}} = util:seconds_to_localtime(Time),
	Content = io_lib:format("~2..0w月~2..0w日~2..0w:~2..0w",[Month, Day, Hour, M]),
	Content.
	
